package Client;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 */
public class Client {

   static protected int setLen = 32;

   String setLength(String str) {
      int thisLen = str.length();
      if(thisLen > this.setLen) {
         return str;
      }
      else {
         int rest = this.setLen - thisLen;
         StringBuffer newStr = new StringBuffer(str);
         for(int i = 0; i < rest; i++) {
            newStr.append(" ");
         }
         return newStr.toString();
      }
   }
   public void clientSendMessage()
   {
      try {
         // 1. 启动客户端，创建客户端Socket，指定服务器地址和端口
         System.out.printf("客户端启动!%n");
         Socket socket = new Socket("127.0.0.1", 8888);
         System.out.printf("创建了客户端Socket!%n");

         // 2. 拼接得到需要传输的字符串
         Scanner scanner = new Scanner(System.in);
         String field0 = scanner.next();
         String field1 = scanner.next();
         String field2 = scanner.next();
         String field3 = scanner.next();
         String field4 = scanner.next();
         String field5 = scanner.next();

         // 3. 向服务端发送输出流
         OutputStream os = socket.getOutputStream(); // 获取字节输出流
         PrintWriter pw = new PrintWriter(os); // 将输出流包装成打印流
         pw.write(field0 + setLength(field1) + setLength(field2) + setLength(field3)+ setLength(field4)+ setLength(field5)+"\n");
         pw.flush();

         // 4.向服务器发送文件
         BufferedInputStream bis = null;
         BufferedOutputStream bos = null;
         if (field0.compareTo("2") == 0) {
            bis = new BufferedInputStream(new FileInputStream("C:/Client/"+field1+"-"+field2+"-"+field3+"-"+field4+"answer.txt"));
         }
         if (field0.compareTo("3") == 0) {
            bis = new BufferedInputStream(new FileInputStream("C:/Client/"+field1+"-"+field2+"-"+field3+"-"+field4+"-"+field5+"solution.txt"));
         }
         if (field0.compareTo("4") == 0) {
            bis = new BufferedInputStream(new FileInputStream("C:/Client/"+field1+"-"+field2+"-"+field3+"-"+field4+"answer.txt"));
         }
         if (field0.compareTo("2") == 0 || field0.compareTo("3") == 0 || field0.compareTo("4") == 0) {
            bos = new BufferedOutputStream(os);
            int input;

            while ((input = bis.read()) != -1) {
               //System.out.printf("正在传输，请等待...%n");
               bos.write(input);
            }
            System.out.printf("传输完成!%n");
            bos.flush();
            bis.close();
         }
         socket.shutdownOutput();
         ///以上是传输，下面不会再传东西了，之后都是接收


         // 5. 获取输入流，读取服务端的响应信息
         InputStream is = socket.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         bis=new BufferedInputStream(is);
         String info = null;
         String returnFromServer = null;
         String file=null;
         // 循环读取服务端信息
         while ((info = br.readLine()) != null) {
            returnFromServer = info.substring(0, 1);
            System.out.printf("服务端返回信息：%s%n", returnFromServer);
            //此处为服务器返回的结果!
            break;
         }
         if (field0.compareTo("5") == 0) {
            System.out.printf("正在接收文字文件%n");
            bos = new BufferedOutputStream(new FileOutputStream(new File("C:/Client/"+field1+"-"+field2+"-"+field3+"-"+field4+"problem.txt")));
         }
         if (field0.compareTo("5") == 0 ) {
            System.out.printf("正在读写文件%n");
            int input=-1;
            while((input=bis.read())!=-1){
               //System.out.printf("正在读写，请稍后...%n");
               bos.write(input);
            }
            bos.flush();
            System.out.printf("读写完成%n");
         }
         System.out.printf("已完成请求，中断连接!%n");
         // 6. 关闭资源
         if (pw != null) {
//            System.out.printf("关闭pw%n");
            pw.close();
         }
         if (bos != null) {
//            System.out.printf("关闭bos%n");
            bos.close();
         }
         if (br != null) {
//            System.out.printf("关闭br%n");
            br.close();
         }
         if (is != null) {
//            System.out.printf("关闭is%n");
            is.close();
         }
         if (os != null) {
//            System.out.printf("关闭os%n");
            os.close();
         }
         if (socket != null){
//            System.out.printf("关闭socket%n");
            socket.close();
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
